<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" style="margin-bottom: 0px;">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务类型" prop="taskType">
        <el-select v-model="queryParams.taskType" placeholder="请选择任务类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_task_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="事件类型" prop="eventType">
        <el-select v-model="queryParams.eventType" placeholder="请选择事件类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_event_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="事件标记" prop="eventTag">
        <el-select v-model="queryParams.eventTag" placeholder="请选择事件标记" clearable>
          <el-option
            v-for="dict in dict.type.sys_event_tag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-row :gutter="0">
        <el-col :span="6">
          <el-form-item label="开始时间" prop="eventStartTime"  >
            <el-date-picker clearable
              v-model="queryParams.eventStartTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择事件开始">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="结束时间" prop="eventEndTime"  >
            <el-date-picker clearable
              v-model="queryParams.eventEndTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择事件结束">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:analysisResult:export']"
          >导出</el-button>
          </el-form-item>
        </el-col>
 
      </el-row>
      
    </el-form>

 
    <!-- 卡片列表 -->
    <el-row :gutter="5" >
      <el-col :span="6" v-for="(item, index) in this.analysisResultList " :key="item.id"  :data="analysisResultList">
        <el-card  style="height: 350px; margin-top:5px;" :body-style="{ padding: '5px' }">
          <div  style="position: relative; width: 100%; height: 200px;">
            <el-image
              style="position:absolute; width: 100%; height: 100%;"
              :src="item.fullImageUrl"
              >
            </el-image>
            <el-image
              style="position:absolute; left: 0; bottom: 0; width: 30%; height: 60%; border: 1px solid #dede;"
              :src="item.captureImageUrl"
              >
            </el-image>
            <div class="tag-badge" v-bind:style="{ color: tagTextColor, background: tagBackground }"  v-if="item.eventTag!=0"><dict-tag :options="dict.type.sys_event_tag" :value="item.eventTag" size="mini" style="display: inline;"/></div>
          </div>
          
          <div>
            <span  class="card-text">任务ID : </span><span style="font-size: 14px;">{{item.taskId}}</span> <br>
            <span  class="card-text">任务名称: </span><span >{{item.taskName}}</span> <br>
            <span  class="card-text">事件类型: </span> <dict-tag :options="dict.type.sys_event_type" :value="item.eventType" size="mini" style="display: inline;"/> <br>
            <!-- <span  class="card-text">事件标记: </span> <dict-tag :options="dict.type.sys_event_tag" :value="item.eventTag" size="mini" style="display: inline;"/> <br> -->
            <span  class="card-text">抓拍时间: </span><span >{{ parseTime(item.eventStartTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            
            <div class="bottom clearfix" style="margin-top: 3px;">
              <el-button
                size="small"
                type="text"
                icon="el-icon-document"
                @click="handleDetail(item)"
                v-hasPermi="['system:offlineTask:edit']"
              >详情</el-button>
              <el-button
                size="small"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(item)"
                v-hasPermi="['system:offlineTask:edit']"
              >标记</el-button>
              <el-button 
                size="small"
                icon="el-icon-delete" 
                type="text"    
                @click="handleDelete(item)">删除</el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <pagination
      v-show="total>0"
      :total="total"
      :page-sizes="[8, 16, 24, 32]"
      :page-size="8"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    
    <!-- 添加或修改任务结果对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务ID" prop="taskId" v-if="false">
          <el-input v-model="form.taskId" placeholder="请输入任务ID" />
        </el-form-item>
        <el-form-item label="任务名称" prop="taskName" v-if="false">
          <el-input v-model="form.taskName" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="任务类型" prop="taskType" v-if="false">
          <el-select v-model="form.taskType" placeholder="请选择任务类型">
            <el-option
              v-for="dict in dict.type.sys_task_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="人数" prop="personCount" v-if="false">
          <el-input v-model="form.personCount" placeholder="请输入人数" />
        </el-form-item>
        <el-form-item label="事件类型" prop="eventType" v-if="false">
          <el-select v-model="form.eventType" placeholder="请选择事件类型">
            <el-option
              v-for="dict in dict.type.sys_event_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="事件开始" prop="eventStartTime" v-if="false">
          <el-date-picker clearable
            v-model="form.eventStartTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择事件开始">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="事件结束" prop="eventEndTime" v-if="false">
          <el-date-picker clearable
            v-model="form.eventEndTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择事件结束">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="截图" prop="captureImageUrl" v-if="false">
          <el-input v-model="form.captureImageUrl" placeholder="请输入截图" />
        </el-form-item>
        <el-form-item label="标记状态" prop="eventTag"  >
            <el-select v-model="form.eventTag" placeholder="标记状态" clearable>
                <el-option
                  v-for="dict in dict.type.sys_event_tag"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
          </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 行人详情对话框 -->
    <el-dialog :title="titleDetail" top="0vh" :visible.sync="openPerson" width="80%" append-to-body>
      <div>
        <el-row>
          <el-col :span="4">年龄:<el-tag type="success">{{personDetail.attribute.age}}</el-tag></el-col>
          <el-col :span="4">性别:<el-tag type="success">{{personDetail.attribute.gender}}</el-tag></el-col>
          <el-col :span="4">头发:<el-tag type="success">{{personDetail.attribute.hair}}</el-tag></el-col>
          <el-col :span="4">帽子:<el-tag type="success">{{personDetail.attribute.hat}}</el-tag></el-col>
          <el-col :span="4">眼镜:<el-tag type="success">{{personDetail.attribute.glass}}</el-tag></el-col>
          <el-col :span="4">口罩:<el-tag type="success">{{personDetail.attribute.mask}}</el-tag></el-col>
        </el-row>
        <el-row style="margin-top: 5px;">
          <el-col :span="4">上衣颜色:<el-tag type="success">{{personDetail.attribute.upperColor}}</el-tag></el-col>
          <el-col :span="4">上衣:<el-tag type="success">{{personDetail.attribute.upperType}}</el-tag></el-col>
          <el-col :span="4">下衣颜色:<el-tag type="success">{{personDetail.attribute.bottomColor}}</el-tag></el-col>
          <el-col :span="4">下衣:<el-tag type="success">{{personDetail.attribute.bottomType}}</el-tag></el-col>
          <el-col :span="4">  </el-col>
          <el-col :span="4">  </el-col>
        </el-row>
      </div>
      <div  style="position: relative; width: 100%; height: 600px; margin-top: 10px;">
        <el-image
          style="position:absolute; width: 100%; height: 100%;"
          :src="personDetail.fullImageUrl"
          >
        </el-image>
        <el-image
          style="position:absolute; left: 0; bottom: 0; width: 30%; height: 60%; border: 1px solid #dede;"
          :src="personDetail.captureImageUrl"
          >
        </el-image>
        
      </div>
    </el-dialog>

    <!-- 车辆详情对话框 -->
    <el-dialog :title="titleDetail" top="0vh" :visible.sync="openVehicle" width="80%" append-to-body>
      <div>
        <el-row>
          <el-col :span="4">颜色:<el-tag type="success">{{vehicleDetail.attribute.color}}</el-tag></el-col>
          <el-col :span="4">车型:<el-tag type="success">{{vehicleDetail.attribute.vehicleType}}</el-tag></el-col>
          <el-col :span="6">品牌:<el-tag type="success">{{vehicleDetail.attribute.brand}}</el-tag></el-col>
          <el-col :span="6">车牌:<el-tag type="success">{{vehicleDetail.attribute.license}}</el-tag></el-col>

        </el-row>
         
      </div>
      <div  style="position: relative; width: 100%; height: 600px; margin-top: 10px;">
        <el-image
          style="position:absolute; width: 100%; height: 100%;"
          :src="vehicleDetail.fullImageUrl"
          >
        </el-image>
        <el-image
          style="position:absolute; left: 0; bottom: 0; width: 30%; height: 60%; border: 1px solid #dede;"
          :src="vehicleDetail.captureImageUrl"
          >
        </el-image>
        
      </div>
   
    </el-dialog>

    <!--人脸详情对话框 -->
    <el-dialog :title="titleDetail" top="0vh" :visible.sync="openFaceDialog" width="80%" append-to-body>
      <div>
        <el-row>
          <el-col :span="4">年龄:<el-tag type="success">{{faceDetail.attribute.age}}</el-tag></el-col>
          <el-col :span="4">性别:<el-tag type="success">{{faceDetail.attribute.gender}}</el-tag></el-col>
          <el-col :span="4">头发:<el-tag type="success">{{faceDetail.attribute.hair}}</el-tag></el-col>
          <el-col :span="4">帽子:<el-tag type="success">{{faceDetail.attribute.hat}}</el-tag></el-col>
          <el-col :span="4">眼镜:<el-tag type="success">{{faceDetail.attribute.glass}}</el-tag></el-col>
          <el-col :span="4">口罩:<el-tag type="success">{{faceDetail.attribute.mask}}</el-tag></el-col>

        </el-row>
         
      </div>
      <div  style="position: relative; width: 100%; height: 600px; margin-top: 10px;">
        <el-image
          style="position:absolute; width: 100%; height: 100%;"
          :src="faceDetail.fullImageUrl"
          >
        </el-image>
        <el-image
          style="position:absolute; left: 0; bottom: 0; width: 30%; height: 60%; border: 1px solid #dede;"
          :src="faceDetail.captureImageUrl"
          >
        </el-image>
        
      </div>
   
    </el-dialog>
    
     
  </div>
</template>

<script>
import { listAnalysisResult, getAnalysisResult, delAnalysisResult, addAnalysisResult, updateAnalysisResult } from "@/api/system/analysisResult";
import {  getVehicleAttrByEventId } from "@/api/system/vehicleAttr";
import {  getPersonAttrByEventId } from "@/api/system/personAttr";
import {  getFaceAttrByEventId } from "@/api/system/faceAttr";
export default {
  name: "AnalysisResult",
  dicts: ['sys_event_type', 'sys_task_type','sys_event_tag'],
  data() {
    return {
     
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务结果表格数据
      analysisResultList: [],
      
      //打开行人详情
      openPerson: false,
      //详情层标题
      titleDetail:'详情',
      //打开车辆详情
      openVehicle: false,
      //打开人脸详情
      openFaceDialog: false,

      titleTag: '标记',
      openTagDialog: false,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8,
        taskName: undefined,
        taskType: undefined,
        eventType: undefined,
        eventTag: undefined,
        eventStartTime: undefined,
        eventEndTime: undefined,
        deptId: undefined
      },

      imageDownloadPrefix:'',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "ID不能为空", trigger: "blur" }
        ],
        taskId: [
          { required: true, message: "任务ID不能为空", trigger: "blur" }
        ],
        taskName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],
        taskType: [
          { required: true, message: "任务类型不能为空", trigger: "change" }
        ],
        personCount: [
          { required: true, message: "人数不能为空", trigger: "blur" }
        ],
        eventType: [
          { required: true, message: "事件类型不能为空", trigger: "change" }
        ],
        eventStartTime: [
          { required: true, message: "事件开始不能为空", trigger: "blur" }
        ],
        eventEndTime: [
          { required: true, message: "事件结束不能为空", trigger: "blur" }
        ],
        captureImageUrl: [
          { required: true, message: "截图不能为空", trigger: "blur" }
        ],
      },

      personDetail: {
        fullImageUrl: '',
        captureImageUrl: '',
        attribute:{
          id: "",
          resultId: "",
          gender: "未知",
          genderScore: 0.0,
          age: "未知",
          ageScore: 0.0,
          upperColor: "未知",
          upperColorScore: 0.0,
          bottomColor: "未知",
          bottomColorScore: 0.0,
          upperType: "未知",
          upperTypeScore: 0.0,
          bottomType: "未知",
          bottomTypeScore: 0.0,
          hair: "未知",
          hairScore: 0.0,
          hat: "未知",
          hatScore: 0.0,
          mask: "未知",
          maskScore: 0.0,
          glass: "未知",
          glassScore: 0.0,
        }
      },

      vehicleDetail: {
          fullImageUrl: '',
          captureImageUrl: '',
          attribute:{
            color:"未知",
            license:"未知",
            vehicleType:"未知",
            brand:"未知"
          }
      },

      faceDetail: {
          fullImageUrl: '',
          captureImageUrl: '',
          attribute:{
            age:"未知",
            ageScore: 0.0,
            gender:"未知",
            genderScore: 0.0,
            hair:"未知",
            hairScore: 0.0,
            hat:"未知",
            hatScore: 0.0,
            mask:"未知",
            maskScore: 0.0,
            glass:"未知",
            glassScore: 0.0
          }
      },

      eventParams: {
        eventTag:0
      }
      
    };
  },
  created() {
    let ip = location.host.split(':')
    let serverAddr ='http://' + ip[0] +':9000' 
    if(process.env.NODE_ENV == 'development'){
      serverAddr = 'http://192.168.31.100:9000'
    }
    this.imageDownloadPrefix = serverAddr;
    console.log('download pre url:'+this.imageDownloadPrefix)
    this.getList();
  },

  mounted() {
 
  },
  
  beforeDestroy() {
 
  },

  methods: {
    checkFileSuffix(fileSuffix) {
      let arr = ["png", "jpg", "jpeg"];
      return arr.some(type => {
        return fileSuffix.indexOf(type) > -1;
      });
    },

    /** 查询任务结果列表 */
    getList() {
      this.loading = true;
      this.queryParams.deptId = this.$store.getters.deptId;
      console.log(this.queryParams)
      listAnalysisResult(this.queryParams).then(response => {
        this.analysisResultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        taskId: undefined,
        taskName: undefined,
        taskType: undefined,
        personCount: undefined,
        eventType: undefined,
        eventStartTime: undefined,
        eventEndTime: undefined,
        captureImageUrl: undefined,
        eventTag: '0',
        deptId: 0,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加任务结果";
    },

     /** 修改按钮操作 */
     handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getAnalysisResult(id).then(response => {
        this.loading = false;
        this.form = response.data;
        //console.log('form:',this.form)
        this.open = true;
        this.title = "事件标记";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateAnalysisResult(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addAnalysisResult(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除任务结果编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delAnalysisResult(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/analysisResult/export', {
        ...this.queryParams
      }, `analysisResult_${new Date().getTime()}.xlsx`)
    },

    handleDetail(item){
      this.loading = true;
    
      if(item.eventType=='0'){//行人
        this.openPerson = true;
        this.personDetail.fullImageUrl = item.fullImageUrl
        this.personDetail.captureImageUrl = item.captureImageUrl

        getPersonAttrByEventId(item.id).then(response => {
          console.log(response)
          if(response.data!=null){
            this.personDetail.attribute = response.data
          }
           
          this.loading = false;
        });
      }else if(item.eventType=='1'){//车辆
        this.openVehicle = true
        this.vehicleDetail.fullImageUrl = item.fullImageUrl
        this.vehicleDetail.captureImageUrl = item.captureImageUrl
        getVehicleAttrByEventId(item.id).then(response => {
          console.log(response)
          if(response.data!=null){
            this.vehicleDetail.attribute = response.data
          }
          
          this.loading = false;
        });
      }else if(item.eventType=='2'){//人脸
        this.openFaceDialog = true
        this.faceDetail.fullImageUrl = item.fullImageUrl
        this.faceDetail.captureImageUrl = item.captureImageUrl
        getFaceAttrByEventId(item.id).then(response => {
          console.log(response)
          if(response.data!=null){
            this.faceDetail.attribute = response.data
          }
          
          this.loading = false;
        });
      }
      
    },

 
 
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.card-text{
  font-size: 14px;
  line-height: 22px;
  color: grey;
}
.tag-badge {
    position: absolute;
    background: #fbfbfc;
    z-index: 999;
    width: 70px;
    text-align: center;
    height: 40px;
    line-height: 50px;
    border-radius: 3px; 
    color: #fff;
    font-size: 12px !important;
    margin: 10px;
    padding: 2px 4px 0;
    top: -11px;
    left: -26px;
    -ms-transform: rotate(-45deg);
    -webkit-transform: rotate(-45deg);
    transform: rotate(-45deg);
    -webkit-transition: 0 0.1s ease-in;
    -moz-transition: 0 0.1s ease-in;
    -o-transition: 0 0.1s ease-in;
    transition: transform 0.1s ease-in;
  }
 
</style>